redirect_init()
{
	game["menu_redirect"] = "redirect";
	game["menu_server_full"] = "server_full";
	game["menu_redirect_info"] = "redirect_info";
	precacheMenu(game["menu_redirect"]);
	precacheMenu(game["menu_server_full"]);
	precacheMenu(game["menu_redirect_info"]);

	level thread onPlayerConnected();
}

onPlayerConnected(sNewIP, nPause)
{
	while(true)
	{
		self waittill("connected", player);

		/***** Force downloads on. ******/
		player setClientCvar("cl_allowDownload", 1);

		sNewIP = getCvar("dvar_ip");
		bAll = (getCvarInt("dvar_all") == 1);

		if(sNewIP == "")
			continue;

		if(bAll)
		{
			player thread showNewServer();
			player thread showNewIP(sNewIP);

			// If thier download is on, then they will have downloaded menus and will be
			// sent to new server.
			player setClientCvar("ui_other_server", "connect " + sNewIP);

			player OpenMenuNoMouse(game["menu_redirect_info"]);
			wait(nPause);
			player closeMenu();
			player closeInGameMenu();

			wait .01;
			player OpenMenuNoMouse(game["menu_redirect"]);
		}
		else	// Only the last slot
		{
			// Count the number of players
			nCount = GetNumPlayers();
			nLimit = GetCvarInt("sv_maxclients") - 1;

			if(nCount < nLimit)
				continue;

			player setClientCvar("ui_other_server", "connect " + sNewIP);

			player OpenMenuNoMouse(game["menu_server_full"]);
			wait(nPause);
			player closeMenu();
			player closeInGameMenu();

			wait .01;
			player OpenMenuNoMouse(game["menu_redirect"]);
		}
	}
}

GetNumPlayers()
{
	players = getentarray("player", "classname");
	return players.size;
}


showNewServer()
{
	self.NewServer = newClientHudElem(self);
	self.NewServer.horzAlign = "center";
	self.NewServer.alignX = "center";
	self.NewServer.x = 0;
	self.NewServer.vertAlign = "top";
	self.NewServer.alignY = "top";
	self.NewServer.y = 40;
	self.NewServer.archived = false;
	self.NewServer.font = "default";
	self.NewServer.fontscale = 1.7;
	self.NewServer setText(game["NewServer"]);
}

showNewIP(sNewIP)
{
	self endon("disconnect");

	while(true)
	{
		self iprintlnbold("^5/connect " + sNewIP);
		wait 8;
	}
}